
#ifndef APP_PD_H
#define APP_PD_H


#include "PD_UFP_Protocol.h"
#include "app_pd_log.h"

#define PD_POLLING              100
#define TYPEC_SINK_WAIT_CAP     1500//350
#define REQUEST_TO_PS_READY     580
#define PPS_REQUEST             5000

typedef uint8_t status_power_t;


enum{
    kPDEvent_PD_Start = 1<<0,  
    kPDEvent_PPS_Start = 1<<1,
    kPDEvent_PD_OUT = 1<<2,
    kPDEvent_PPS_OUT  = 1<<3,
};

#define kPDEvent_RECV    (kPDEvent_PD_Start|kPDEvent_PPS_Start|kPDEvent_PD_OUT|kPDEvent_PPS_OUT)


enum {
    STATUS_LOG_MSG_TX,
    STATUS_LOG_MSG_RX,
    STATUS_LOG_DEV,
    STATUS_LOG_CC,
    STATUS_LOG_SRC_CAP,
    STATUS_LOG_POWER_READY,
    STATUS_LOG_POWER_PPS_STARTUP,
    STATUS_LOG_POWER_REJECT,
    STATUS_LOG_LOAD_SW_ON,
    STATUS_LOG_LOAD_SW_OFF,
};

enum {
    STATUS_POWER_NA = 0,
    STATUS_POWER_TYP,
    STATUS_POWER_PPS
};

enum {
    kPDState_Start = 0,
    kPDState_PPSStart,
    kPDState_PDO,
    kPDState_PPSO,
    kPDState_Stop,
    
};

typedef struct
{
    uint8_t state;
    uint8_t pd_ic_initial;
    PD_protocol_t protocol;
    
    // Power ready power
    uint16_t ready_voltage;
    uint16_t ready_current;
    
    // PPS setup
    uint16_t PPS_voltage_next;
    uint8_t PPS_current_next;
    // Status
    uint8_t status_initialized;
    uint8_t status_src_cap_received;
    status_power_t status_power;
    // Timer and counter for PD Policy
    uint32_t time_polling;
    uint32_t time_wait_src_cap;
    uint32_t time_wait_ps_rdy;
    uint32_t time_PPS_request;
    uint8_t get_src_cap_retry_count;
    uint8_t wait_src_cap;
    uint8_t wait_ps_rdy;
    uint8_t send_request;
    //add 2024/10/30
    uint8_t cc;
    char power_list[7][24];
    uint8_t power_list_num;
    uint8_t power_index;
    float power_v;
    float power_i;
}App_PD_t;

extern App_PD_t app_pt;
extern uint8_t pd_get_power_list(void);

#endif
